FAISS (Facebook AI Similarity Search)

FAISS is an open-source library developed by Facebook AI for efficient similarity search and clustering of dense vectors. It is highly optimized for handling very large dataset of high-dimensional vectors.

Key Features:

Use Case:

FAISS can be used in scenarios like image search, recommendation systems, and NLP embedding retrieval.

#-------------------------------------------------------------#
# pip install faiss-cpu  # or 'faiss-gpu' for GPU version
#-------------------------------------------------------------#
import faiss
import numpy as np

# Create an index for L2 distance (Euclidean)
d = 128  # dimension of vectors
index = faiss.IndexFlatL2(d)  # create the index

# Generate random data (vectors)
vectors = np.random.random((1000, d)).astype('float32')

# Add vectors to the index
index.add(vectors)

# Query for the nearest neighbors
query_vector = np.random.random((1, d)).astype('float32')
distances, indices = index.search(query_vector, k=5)  # retrieve top-5 nearest neighbors
print("Nearest neighbors:", indices)